package org.example.new_hbps_backend_reconstruction.controller.patient;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.new_hbps_backend_reconstruction.common.results.R;
import org.example.new_hbps_backend_reconstruction.service.CalibrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 丁利军
 * @date 2025年10月31日 14:45
 * @description 心永设备WebSocket标定管理
 */

@RestController
@RequestMapping("/websocket")
@Tag(description = "心永设备WebSocket标定管理(暂时不用)", name = "心永设备WebSocket标定管理")
public class CalibrationController {

    @Autowired
    private CalibrationService calibrationService;

    @Operation(summary = "获取WebSocket连接URL")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功")
    })
    @GetMapping("/getConnectionUrl")
    public R<String> getWebSocketUrl(@RequestParam String userAccessToken) {
        String url = "wss://open.heart-forever.com/api/ext/websocket/" + userAccessToken;
        return R.ok(url, "获取WebSocket连接地址成功");
    }

    @Operation(summary = "开始标定")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "标定开始成功"),
            @ApiResponse(responseCode = "500", description = "标定开始失败")
    })
    @PostMapping("/startCalibration")
    public R<String> startCalibration(@RequestParam String sessionId) {
        try {
            calibrationService.startCalibration(sessionId);
            return R.ok("标定开始成功");
        } catch (Exception e) {
            return R.failed("标定开始失败: " + e.getMessage());
        }
    }

    @Operation(summary = "结束标定")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "标定结束成功"),
            @ApiResponse(responseCode = "500", description = "标定结束失败")
    })
    @PostMapping("/stopCalibration")
    public R<String> stopCalibration(@RequestParam String sessionId) {
        try {
            calibrationService.stopCalibration(sessionId);
            return R.ok("标定结束成功");
        } catch (Exception e) {
            return R.failed("标定结束失败: " + e.getMessage());
        }
    }
}
